package com.grechur.wanandroidcompose.viewmodel

import android.app.Application
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.setValue
import androidx.lifecycle.AndroidViewModel
import com.grechur.wanandroidcompose.bean.article.Article
import com.grechur.wanandroidcompose.bean.article.PageData
import com.grechur.wanandroidcompose.bean.common.CommonWeb
import com.grechur.wanandroidcompose.http.NetworkRequest
import com.grechur.wanandroidcompose.http.service.SearchService

class SearchViewModel(application: Application) : AndroidViewModel(application) {

    private val searchService = NetworkRequest.getService(getApplication(),SearchService::class.java)

    var hotList by mutableStateOf(Result.success(listOf<Article>()))

    var searchList by mutableStateOf(Result.success(listOf<Article>()))

    var commonList by mutableStateOf(Result.success(listOf<CommonWeb>()))

    var preKey = ""

    suspend fun getHotKey() {
        val result = try {
            val hotKey = searchService.getHotKey()
                val default = hotList.getOrDefault(listOf())
                val let = if (hotKey.errorCode == 0) {
                    hotKey.data.takeIf { it != null }?.let { articles ->
                        Result.success(articles)
                    }?: Result.success(default)
                } else {
                    Result.failure(IllegalArgumentException("error code ${hotKey.errorCode} error msg ${hotKey.errorMsg}"))
                }
                let
        } catch (e:Exception) {
            Result.failure(e)
        }
        hotList = result
    }

    suspend fun search(index:Int?,key:String?) {
        val result = try {
            if (preKey != key) {
                searchList = Result.success(listOf())
                preKey = key?:""
            }
            val search = searchService.search(index, key)
            if (search.errorCode == 0) {
                val toMutableList = searchList.getOrDefault(listOf()).toMutableList()
                search.data.takeIf { it != null }?.let { pageData: PageData ->
                    pageData.datas.takeIf { !it.isNullOrEmpty() }?.let { articles: List<Article> ->
                        toMutableList.addAll(articles)
                        Result.success(toMutableList)
                    } ?: Result.success(toMutableList)
                } ?: Result.success(toMutableList)
            } else {
                Result.failure(IllegalArgumentException("error code ${search.errorCode} error msg ${search.errorMsg}"))
            }
        } catch (e: Exception) {
            Result.failure(e)
        }
        searchList = result
    }

    suspend fun friend() {
        val result = try {
            val hotKey = searchService.friend()
            val default = commonList.getOrDefault(listOf())
            val let = if (hotKey.errorCode == 0) {
                hotKey.data.takeIf { it != null }?.let { articles ->
                    Result.success(articles)
                }?: Result.success(default)
            } else {
                Result.failure(IllegalArgumentException("error code ${hotKey.errorCode} error msg ${hotKey.errorMsg}"))
            }
            let
        } catch (e:Exception) {
            Result.failure(e)
        }
        commonList = result
    }
}